package com.erp.dao;

import com.erp.bean.Category;
import com.erp.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author tj
 * @Date 2022-06-01 15:55:40
 */
public class CategoryDao {
    JDBCUtils jdbcUtils = new JDBCUtils();

    // list 列表
    public List<Category> list() {
        List<Category> list = new ArrayList<>();

        try {
            PreparedStatement pstm = null;
            ResultSet rs = null;
            Connection connection = jdbcUtils.getConnection();
            if (connection != null) {
                String sql = "select * from category";
                Object[] params = null;
                rs = jdbcUtils.execute(connection, sql, params, rs, pstm);
                while (rs.next()) {
                    Category category = new Category();
                    category.setId(rs.getInt("id"));
                    category.setName(rs.getString("name"));
                    category.setDesciption(rs.getString("description"));
                    category.setMark(rs.getString("mark"));
                    category.setCreatedTime(rs.getObject("created_time", LocalDateTime.class));
                    list.add(category);
                }
                jdbcUtils.closeResource(connection, pstm, rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }

    // 新增
    public boolean save(Category category) {
        boolean saveRet = false;
        try {
            Connection connection = jdbcUtils.getConnection();
            if (connection != null) {
                String sql = "insert into category (name,description,mark,created_time) value (?,?,?,?)";

                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, category.getName());
                preparedStatement.setString(2, category.getDesciption());
                preparedStatement.setString(3, category.getMark());
                preparedStatement.setObject(4, category.getCreatedTime());

                int ret = preparedStatement.executeUpdate();
                if (ret > 0) {
                    saveRet = true;
                }
                jdbcUtils.closeResource(connection, preparedStatement, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return saveRet;
    }

    // 修改
    public boolean edit(Category category) {
        boolean saveRet = false;
        try {
            Connection connection = jdbcUtils.getConnection();
            if (connection != null) {
                String sql = "update  category set name = ?,description = ?,mark=? where id = ?";

                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, category.getName());
                preparedStatement.setString(2, category.getDesciption());
                preparedStatement.setString(3, category.getMark());
                preparedStatement.setInt(4, category.getId());

                int ret = preparedStatement.executeUpdate();
                if (ret > 0) {
                    saveRet = true;
                }
                jdbcUtils.closeResource(connection, preparedStatement, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return saveRet;
    }

    // 删除
    public boolean delete(Integer id) {
        boolean saveRet = false;
        try {
            Connection connection = jdbcUtils.getConnection();
            if (connection != null) {
                String sql = "delete from category where id = ?";

                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setInt(1, id);

                int ret = preparedStatement.executeUpdate();
                if (ret > 0) {
                    saveRet = true;
                }
                jdbcUtils.closeResource(connection, preparedStatement, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return saveRet;
    }
}